$(function () {

    getReviewers({
        totalAmount: 0
    });

    // 初始化页面上的 datetimepicker
    insDatetimePicker('.is-date');

    layui.use(['form', 'layer'], function () {
        var form = layui.form(),
            layer = layui.layer,
            total_amount = 0,
            account_payable = 0;

        // 监听借款信息中是否借款选项是否开启
        form.on('select(is-loan)', function (data) {
            if (data.value == '是') {
                $('.loan-table .loan-bill').attr('disabled', false);
                $('.loan-table .loan-amount').attr('disabled', false);
            } else {
                // 如果借款为否则清空值并禁用组件
                $('.loan-table .loan-bill').val('');
                $('.loan-table .loan-amount').val('');
                $('.loan-table .loan-bill').attr('disabled', true);
                $('.loan-table .loan-amount').attr('disabled', true);
                // 当是否借款选项选择否时，重新计算金额
                listenLoanAmountChange(0);
            }
        });

        // 添加明细项
        $('#add-detail-item').click(function () {
            // 获取明细数量
            var item_qutatity = $(this).prev().find('tbody tr').length;
            // 去除最后一列合计的行
            item_qutatity = item_qutatity - 2;
            var html = '\n                        <tr>\n                            <td class="valign-middle">' + (item_qutatity + 1) + '</td>\n                            <td class="valign-middle">\n                                <input type="text" name="details[' + (item_qutatity + 1) + '][use]" lay-verify="required" class="layui-input">\n                            </td>\n                            <td class="valign-middle">\n                                <input type="text" name="details[' + (item_qutatity + 1) + '][amount]" lay-verify="required|money" class="layui-input is-money">\n                            </td>\n                            <td>\n                                <button type="button" class="btn btn-danger btn-sm remove-detail-item">\u5220\u9664</button>\n                            </td>\n                        </tr>\n                        ';
            $(this).prev().find('tbody tr:nth-last-child(2)').before(html);
            // 添加明细后更新form组件避免出现无响应或渲染失败为空等现象
            form.render();
        });

        // 删除明细项
        $('.fybx-items-table').on('click', '.remove-detail-item', function () {
            var tr_len = $('.fybx-items-table tbody').children().length;
            // 当移除明细时检查是否小于 3 项，其中 2 项为合计栏位必须保留
            if (tr_len <= 3) {
                layer.msg('至少保留一项明细', {
                    icon: 0,
                    time: 2000
                });
            } else {
                $(this).parent().parent().remove();
                // 当明细项被删除后重新计算小计金额
                listenSubtotalChange();
                // 修正明细项删除后的序列错误
                $('.fybx-items-table tbody tr').each(function (index) {
                    // 排除合计栏位的序列
                    if (index + 1 < tr_len - 2) {
                        $(this).find('td:first-child').text(index + 1);
                    }
                });
            }
        });

        // 监听金额变化
        $('.fybx-items-table').bind('input propertychange', '.is-money', function (e) {
            listenSubtotalChange();
        });

        // 监听借款金额变化
        $('.loan-amount').bind('input propertychange', function (e) {
            // 获取到当前值
            var current_val = $(e.target).val();
            listenLoanAmountChange(current_val);
        });

        // 监听借款金额变化
        function listenLoanAmountChange(loan_amount) {
            if (total_amount) {
                var ap = parseFloat(total_amount - loan_amount);
                // 对应栏位赋值(大写)
                $('.fybx-items-table tbody tr .uppercase-account-payable').val(Arabia_to_Chinese(ap));
                // 对应栏位赋值(小写)
                $('.fybx-items-table tbody tr .account-payable').val(moneyFormat(ap));
            }
        }

        // 监听金额变化
        function listenSubtotalChange() {
            var tmp_total_amount = 0;
            $('.fybx-items-table .is-money').each(function (index) {
                var cur_val = $(this).val();
                tmp_total_amount += parseFloat(cur_val ? cur_val : 0);
            });

            total_amount = tmp_total_amount;
            var loan_amount = $('.loan-amount').val(); // 获取到借款金额
            account_payable = loan_amount ? parseFloat(total_amount - loan_amount) : total_amount; // 如果借款金额存在则用合计金额减去借款金额

            // 将金额转换成大写格式
            var uppercase_total_amout = Arabia_to_Chinese(total_amount),
                uppercase_account_payable = Arabia_to_Chinese(account_payable);

            // 格式化金额（小写）
            $('.fybx-items-table .total-amount').val(moneyFormat(total_amount));
            $('.fybx-items-table .account-payable').val(moneyFormat(account_payable));

            // 格式化金额（大写）
            $('.fybx-items-table .uppercase-total-amount').val(uppercase_total_amout);
            $('.fybx-items-table .uppercase-account-payable').val(uppercase_account_payable);

            throttle(getReviewers, null, approvalConfigs.delay, {
                totalAmount: total_amount
            });
        }

    });
});